package wili.attrMapping.core;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import wili.attrMapping._user.b1.AttConst.Bugs;
import wili.attrMapping._user.b1.AttMapingRule_main;
import wili.c.Ca;
import wili.json.JsonLeaf;
import wili.json.JsonNode;

/**
 * 
 * @author weila 2022年11月6日
 */
public class ParseOutNewAttValue9 {
	public static Set<AttsFile> parseOutNewAttValue_fromOldFile(boolean isCheck, AttsFile oldFile,
			AttsDefine attsDefine, ValueMapingNode ruleRoot, AttMapingRule_main cont) {
		/*
		 * update: 找到所有的旧属性序列、把每个旧属性序列，合并到规则树， 若规则先行无子，意味着：非预期的属性序列（规则路径）。
		 * 
		 */
		Set<AttsFile> fileSet = new HashSet<>();
		UserConf conf = attsDefine.conf;
		AttsDefine def = conf.getAttsDefine();
		List<Atts> oldAttsList = conf.findOldAttsList(def, oldFile);
		List<String> attList = attsDefine.attNames;
		Collections.sort(attList);// 顺序重要，便于同实例同步。
		//
		Ca.pause();
		String targetAtt = conf.getAttsDefine().targetAtt;
		for (int k = 0; k < oldAttsList.size(); k++) {
			++inc;
			// 根据对象名取得旧对象集、对每个对象，取得其属性集。
			Atts oldAtts = oldAttsList.get(k);
			AttMapingRule_main.oldNum++;
			if (isCheck) {
			} else {// 行至规则叶后，取得新属性对象，（叶值+旧属性）~>新属性值，把新属性值刷新到对象中。
				// ValueMapingNode ruleLeaf = ruleRoot.addOrMergeAtts(oldAtts, false);
				if ("847c4445-12nn-35c0-a789-b59bb1fc4a20".equals(oldAtts.key))
					Ca.pause();
				List<Atts> newAttsList = oldAtts.findNewAttsList();
				if (newAttsList.isEmpty()) {
					if (inc < 5)
						Att9.log7("\n WARN_", Bugs.MappingToNotFound, oldAtts);
					continue;
				} //
				String oldKind = oldAtts.getKind();
				for (Atts newAtts : newAttsList) {
					if (!oldKind.equals(newAtts.getKind()))
						Ca.asert(false, "never");
					conf.valueOfNewAtts(oldAtts, ruleRoot, newAtts);// <<<<<<<<<<<<<<<<<<<<<<<<<
					fileSet.add(newAtts.file);
					Ca.asert(newAtts.list == null, "todo");
					if (newAtts.updateValue != null) {
						JsonNode newObj = newAtts.getObject();
						newObj.replaceBy(new JsonLeaf(targetAtt, newAtts.updateValue));
					}
				}
			}
		}
		return fileSet;
	}

	public static int inc;
}
